<?php
namespace App\Http\Controllers;

use App\Enums\UserEnum;
use Dingo\Api\Routing\Helpers;
use Illuminate\Routing\Controller;
use Dingo\Api\Http\Request;

class BaseController extends Controller
{
    use Helpers;

    protected $user_info = [];

    public function __construct(Request $request) {
        if(!config('app.app_start')) {
            echo json_encode([
                'success'       => false,
                'message'       => '系统升级中，请稍后购买',
                'code'          => 500,
                'data'          => []
            ], JSON_UNESCAPED_UNICODE);
            die();
        }

        $user_info = $request -> user();

        if(empty($user_info)){
            return $this->error('未登录');
        }

        if($user_info && $user_info -> status == UserEnum::USER_DISABLED) {
            return $this->error('用户已被冻结');
        }

        $this->user_info = $user_info;
    }

    protected function success($data = [], $message = "OK", $code = 200)
    {
        return $this->response->array([
            'success'   => true,
            'data'      => $data,
            'message'   => $message,
            'code'      => $code
        ]);
    }

    protected function error($message = "操作失败", $code = -1)
    {
        return $this->response->array([
            'success'   => false,
            'data'      => [],
            'message'   => $message,
            'code'      => $code
        ]);
    }
}
